//
//  CollectionScrollView.swift
//  SmallParts
//
//  Created by 航汇聚 on 2019/10/11.
//  Copyright © 2019 BellaZhou. All rights reserved.
//

import UIKit

class CollectionScrollView: UICollectionView {
    
    var timer: Timer?
    let duration = 3.0
    var indexOfPage = 0
    var itemCount = 10
    
    let layout = UICollectionViewFlowLayout.init()
    
    func initConfigCollection() {
        
        layout.itemSize = CGSize(width: self.bounds.size.width, height: self.bounds.size.height)
        layout.minimumLineSpacing = 0.0
        self.collectionViewLayout = layout
        layout.scrollDirection = UICollectionView.ScrollDirection.horizontal
        
        self.dataSource = self
        self.delegate = self
        self.register(UINib.init(nibName: "CollectionScrollCell", bundle: nil), forCellWithReuseIdentifier: "collectionScrollCell")
        self.isPagingEnabled = true
    }
    func autoScrollByDuration(_ duration: TimeInterval) {
        let offsetHeight = self.bounds.size.height
        let offsetWidth = self.bounds.size.width
        let countOfScrollContentSubViews = itemCount
        timer = Timer.scheduledTimer(withTimeInterval: duration, repeats: true) { (timer) in
            var offsetPoint = CGPoint(x: CGFloat(Float(offsetWidth)*Float(self.indexOfPage)), y: 0)
            if self.layout.scrollDirection == UICollectionView.ScrollDirection.vertical {
                offsetPoint = CGPoint(x: 0, y: CGFloat(Float(offsetHeight)*Float(self.indexOfPage)))
            }
            if self.indexOfPage > countOfScrollContentSubViews-1 {
                self.indexOfPage = 0
                offsetPoint = CGPoint(x: 0, y: 0)
                self.contentOffset = offsetPoint
            }else {
                self.scrollRectToVisible(CGRect(x: offsetPoint.x, y: offsetPoint.y, width: offsetWidth, height: offsetHeight), animated: true)
            }
            self.indexOfPage += 1
        }
        timer!.fire()
    }
}
extension CollectionScrollView: UIScrollViewDelegate, UICollectionViewDelegate {
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        timer?.invalidate()
    }
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        self.indexOfPage = Int(scrollView.contentOffset.x/scrollView.bounds.size.width)
        self.autoScrollByDuration(duration)
    }
}
extension CollectionScrollView: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionScrollCell", for: indexPath) as! CollectionScrollCell
        if indexPath.item % 2 == 0 {
            cell.backgroundColor = UIColor.blue
        }else {
            cell.backgroundColor = UIColor.green
        }
        cell.testLabel.text = "collection实现轮播 \(indexPath.section) - \(indexPath.item)"
        return cell
    }
}
